package com.tdam2011.grupo05.utils.parser;

import java.io.IOException;
import java.io.StringReader;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Xml;

import com.google.common.collect.Lists;

public class XMLParser {

	public static final String TAG = "XMLParser";
	public static final String TAG_RESULT = "result";
	public static final String TAG_USER = "user";
	public static final String TAG_DETAIL = "detail";
	public static final String TAG_MESSAGELIST = "messages-list";
	public static final String TAG_MESSAGE = "message";
	public static final String ATT_ID = "id";
	public static final String ATT_TYPE = "type";
	public static final String ATT_USERNAME = "username";
	public static final String ATT_DESCRIPTION = "description";
	public static final String ATT_FROM = "from";
	public static final String ATT_CODE = "code";
	public static final String ATT_TIMESTAMP = "timestamp";
	public static final String SUCCESS_RESULT = "success";
	public static final String ERROR_RESULT = "error";
	public static final String ID = "REQUEST_RANDOM_VALUE";
	public String NAMESPACE = null;

	public Entry parse(StringReader reader) throws XmlPullParserException,
			IOException {
		XmlPullParser parser = Xml.newPullParser();
		parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
		parser.setInput(reader);
		parser.nextTag();
		return readResponseEntry(parser);

	}

	private Entry readResponseEntry(XmlPullParser parser)
			throws XmlPullParserException, IOException {
		Entry entry = new Entry();
		entry.id = parser.getAttributeValue(null, ATT_ID);
		entry.type = parser.getAttributeValue(null, ATT_TYPE);
		parser.require(XmlPullParser.START_TAG, NAMESPACE, TAG_RESULT);
		while (parser.next() != XmlPullParser.END_TAG) {
			if (parser.getEventType() != XmlPullParser.START_TAG) {
				continue;
			}
			String name = parser.getName();
			if (name.equalsIgnoreCase(TAG_DETAIL)) {
				entry.detail = parser.getAttributeValue(null, ATT_DESCRIPTION);
				entry.detail += " "
						+ parser.getAttributeValue(null, ATT_DESCRIPTION);
				entry.code = Integer.parseInt(parser.getAttributeValue(null,
						ATT_CODE));
			} else if (name.equalsIgnoreCase(TAG_USER)) {
				entry.user = parser.getAttributeValue(null, ATT_USERNAME);
			} else if (name.equalsIgnoreCase(TAG_MESSAGELIST)) {
				entry.messages = readMessages(parser);
			} else {
				skip(parser);
			}
		}
		if (entry.detail == null)
			entry.detail = "null";
		if (entry.result == null)
			entry.result = "null";
		if (entry.user == null)
			entry.user = "null";
		return entry;
	}

	private List<Message> readMessages(XmlPullParser parser)
			throws XmlPullParserException, IOException {

		parser.require(XmlPullParser.START_TAG, NAMESPACE, TAG_MESSAGELIST);
		List<Message> ms = Lists.newLinkedList();
		while (parser.next() != XmlPullParser.END_TAG || parser.getDepth() > 2) {
			if (parser.getEventType() != XmlPullParser.START_TAG) {
				continue;
			}
			String name = parser.getName();
			if (name.equals(TAG_MESSAGE)) {
				Message m = new Message();
				m.from = parser.getAttributeValue(null, ATT_FROM);
				m.time = parser.getAttributeValue(null, ATT_TIMESTAMP);
				parser.next();
				m.message = parser.getText();
				ms.add(m);
			} else {
				skip(parser);
			}
		}

		return ms;
	}

	private void skip(XmlPullParser parser) throws XmlPullParserException,
			IOException {
		if (parser.getEventType() != XmlPullParser.START_TAG) {
			throw new IllegalStateException();
		}
		int depth = 1;
		while (depth != 0) {
			switch (parser.next()) {
			case XmlPullParser.END_TAG:
				depth--;
				break;
			case XmlPullParser.START_TAG:
				depth++;
				break;
			}
		}
	}

	/**
	 * Clase usada para procesar el equiemeeles
	 * 
	 * @author martin
	 * 
	 */
	public static class Entry {
		String id;
		String type;
		String detail;
		int code = -1;
		String result;
		String user;
		List<Message> messages;

		public Entry() {
		}

		public String getDetail() {
			return detail;
		}

		public String getId() {
			return id;
		}

		public String getType() {
			return type;
		}

		public String getUser() {
			return user;
		}

		public List<Message> getMessages() {
			return messages;
		}

		public int getCode() {
			return code;
		}

		@Override
		public String toString() {
			return "id=" + id + " type=" + type + " detail=" + detail
					+ " type=" + result + " user=" + user + " messages="
					+ messages;
		}
	}

	public static class Message {
		String time;
		String from;
		String message;

		public String getFrom() {
			return from;
		}

		public String getMessage() {
			return message;
		}

		public String getTime() {
			return time;
		}

		@Override
		public String toString() {

			return "time=" + time + " from=" + from + " message=" + message;
		}
	}

}
